package com.web.oa.controller;

import com.web.oa.pojo.ActiveUser;
import com.web.oa.pojo.BaoxiaoBill;
import com.web.oa.service.BaoxiaoService;
import com.web.oa.service.WorkFlowService;
import com.web.oa.utils.Constants;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class BaoxiaoController {
    @Autowired
    private BaoxiaoService baoxiaoService;
    @Autowired
    private WorkFlowService workFlowService;
    /**我的报销单*/
    @RequestMapping("/myBaoxiaoBill")
    public String myBaoxiaoBill(Model model, HttpSession session){
        //先获取AcitiviUser
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        //根据登录id来获取内容
        List<BaoxiaoBill> list=baoxiaoService.findBaoxiaoByUserList(activeUser.getId());
        //将获取的内容传回给baoxiaoList
        model.addAttribute("baoxiaoList",list);
        return "baoxiaobill";
    }

    /**查看我的审核记录*/
    @RequestMapping("/viewHisComment")
    public String viewHisComment(String id,ModelMap model){
        BaoxiaoBill bill=baoxiaoService.findBaoxiaoBillId(Long.valueOf(id));
        String BUSSINESS_KEY=Constants.BAOXIAO_KEY+"."+bill.getId();
        System.out.println("BUSSINESS_KEY:"+BUSSINESS_KEY);
        final List<Comment> comments=workFlowService.findCommentListBillById(BUSSINESS_KEY);

        model.addAttribute("baoxiaoBill",bill);
        model.addAttribute("commentList",comments);
        return "workflow_commentlist";
    }

    /**强制删除部署信息流程*/
    @RequestMapping("/delDeployment")
    public String delDeployment(String deploymentId){
        this.workFlowService.deleteDeploymentById(deploymentId);
        return "redirect:processDefinitionList";
    }

    /**删除我的报销单中的记录*/
    @RequestMapping("/leaveBillAction_delete")
    public String leaveBillAction_delete(String id){
        this.workFlowService.deleteBaoxiaoBillById(id);
        return "redirect:myBaoxiaoBill";
    }

    /**我的待办事务中的修改操作*/
    @RequestMapping("/updatePermission")
    public String updatePermission(String taskId,Model model){
        //1.先查询报销申请
        //先获取任务ID获取对应的报销单对象
        BaoxiaoBill baoxiaoBill=this.workFlowService.findBaoxiaoBillByTaskId(taskId);
        //将请假单信息添加到model对象中
        model.addAttribute("baoxiaoBill",baoxiaoBill);
        //返回给前端页面
        return "apply_baoxiao";
    }
}
